package com.dgut.controller;

import com.dgut.domain.User;
import com.dgut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String firstPage() {
        System.out.println("转到登录页面");
        return "login";
    }
    @RequestMapping("logout")
    public String loginOut(HttpServletRequest request){
        System.out.println("退出登录");
        HttpSession session = request.getSession();
        session.invalidate();
        return "login";
    }

    /**
     * 用户登录
     * @param request
     * @param user
     * @param code
     * @return
     */
    @RequestMapping("login")
    public ModelAndView Userlogin(HttpServletRequest request, User user, String code) {
        System.out.println("接收到的user：" + user);
        HttpSession se = request.getSession();
        String rcode = (String) se.getAttribute("RANDOMVALIDATECODEKEY");
        System.out.println("验证码：" + rcode);
        System.out.println("输入的验证码：" + code);
        User user1 = new User();
        user1.setName(user.getName());
        user1.setPassword(user.getPassword());
//        UserService service = new UserServiceImpl();
//        判断验证码
        ModelAndView modelAndView = new ModelAndView();
//       输入空字符
        if(code == "" || user.getName() == "" || user.getPassword() ==""){
            modelAndView.addObject("username",user.getName());
            modelAndView.addObject("msg","不能输入空字符");
            System.out.println("不能输入空字符");
            modelAndView.setViewName("login");
            return modelAndView;

        }

        if (code.equalsIgnoreCase(rcode)) {
            Integer id = userService.findUser(user1);
            System.out.println("用户的id是："+id);
            if (id != null) {
                System.out.println("成功");
                User myUser = userService.findUserByName(user.getName());
                System.out.println("登录账号是："+myUser);
                se.setAttribute("myUser",myUser);
//                modelAndView.addObject("useId",id);
//                HttpSession session = request.getSession();
//                获得所有用户id列表
                List<Integer> ids = userService.getUserIdLIst();
                se.setAttribute("ids",ids);
                se.setAttribute("userId",id);
                modelAndView.setViewName("redirect:GetCustomerList");

            } else {
                modelAndView.addObject("username",user.getName());
                modelAndView.addObject("msg","用户名或密码错误");
                System.out.println("用户名或密码错误");
                modelAndView.setViewName("login");
            }
        } else {
            request.setAttribute("W", "验证码错误");
            modelAndView.addObject("username",user.getName());
            modelAndView.addObject("msg","验证码错误");
            System.out.println("验证码错误");
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    /**
     * 用户注册
     * @param name
     * @param password1
     * @param password2
     * @return
     */
    @RequestMapping("register")
    public ModelAndView registerUser(String name, String password1, String password2){
        System.out.println("注册账户"+name +password1+password2);
        ModelAndView modelAndView = new ModelAndView();
//       输入空字符
        if( name == "" || password1 ==""){
            modelAndView.addObject("msg","不能输入空字符");
            System.out.println("不能输入空字符");
            return modelAndView;
        }
//       输入非法长度
        System.out.println("name长度"+name.length());
        System.out.println("ps长度"+password1.length());
        if( name.length() > 20 || password1.length() > 20){
            modelAndView.addObject("msg","不能超过20个字符");
            System.out.println("不能超过20个字符");
            return modelAndView;
        }

//        判断用户名是否存在
        if(userService.ifNameExit(name)){
            modelAndView.addObject("msg","用户名已存在，请重新输入");

        }else{
            if(password1.equals(password2)){
                userService.insertUser(name,password1);
                modelAndView.addObject("msg","注册成功，请登录");
                modelAndView.setViewName("login");
            }else {
                modelAndView.addObject("name",name);
                modelAndView.addObject("msg","两次密码输入不相同，请重新输入");
            }
        }

        return modelAndView;
    }

    @RequestMapping("getUserIldList")
    public ModelAndView getUserIdList(HttpServletRequest request){

        List<User> users = userService.findAllUser();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("users",users);
        modelAndView.setViewName("insertCustomer");
        return modelAndView;
    }

    @RequestMapping("getUserMessage")
    public ModelAndView getUserMessage(String name){
        ModelAndView modelAndView = new ModelAndView();
        User myUser = userService.findUserByName(name);
        System.out.println("aaa登录账号是："+myUser);

        modelAndView.addObject("user",myUser);
        modelAndView.setViewName("userMessage");
        return modelAndView;
    }
    @RequestMapping("updateUserMes")
    public ModelAndView updateUserMes(String name){
        ModelAndView modelAndView = new ModelAndView();
        User myUser = userService.findUserByName(name);
        System.out.println("aaa登录账号是："+myUser);

        modelAndView.addObject("user",myUser);
        modelAndView.setViewName("updateUser");
        return modelAndView;
    }

    /**
     * 修改用户信息
     * @param user
     * @param oldname
     * @param request
     * @return
     */
    @RequestMapping("updateUser")
    public ModelAndView updateUser(User user, String oldname, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("要修改的用户是："+user);
//        user.setCustomers(oldcustomers);
        HttpSession session = request.getSession();
        User myUser = (User) session.getAttribute("myUser");
        System.out.println("aaaaaa"+myUser);
        user.setCustomers(myUser.getCustomers());
        if(user.getName().equals(oldname)){
            userService.updateUser(user);
            myUser.setName(user.getName());
            session.setAttribute("myUser",myUser);
            modelAndView.setViewName("userMessage");
        }else{
            if(userService.ifNameExit(user.getName())){
                modelAndView.addObject("nameMsg","用户名已存在，请重新输入");
                modelAndView.addObject("user",user);
                modelAndView.setViewName("updateUser");
            }else {
                userService.updateUser(user);
                myUser.setName(user.getName());
                session.setAttribute("myUser",myUser);
                modelAndView.setViewName("userMessage");
            }
        }


        return modelAndView;
    }
}
